package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.QuestionBank;
import com.example.demo.entity.Teacher;
import com.example.demo.entity.User;
import com.example.demo.mapper.QuestionBankMapper;
import com.example.demo.service.ICourseService;
import com.example.demo.service.IQuestionBankService;
import com.example.demo.service.IQuestionTypeService;
import com.example.demo.service.ITeacherService;
import com.example.demo.utils.AjaxJson;
import com.example.demo.utils.UserContext;
import com.example.demo.vo.QuestionBankVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 题库表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-11-10
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements IQuestionBankService {

    @Autowired
    ITeacherService teacherService;

    @Autowired
    ICourseService courseService;

    @Autowired
    IQuestionTypeService questionTypeService;

    @Override
    public AjaxJson updataTeacherId(QuestionBank questionBank, Integer userId) {
        Teacher teacher = teacherService.getByUserId(userId);
        Integer teacherId = teacher.getTeacherId();
        QuestionBank teacher_questionBank = this.getOne(new QueryWrapper<QuestionBank>().eq("teacher_id", teacherId).eq("question_id", questionBank.getQuestionId()));
        if (teacher_questionBank == null) {
            return this.updateById(questionBank) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
        }
        return AjaxJson.getError("更新成功");
    }

    /**
     * TODO 又是连表查询，对于二级查询的内容做缓存，题目可能多变但题型、课程基本不变，缓存！！！
     * @return
     */
    @Override
    public List<QuestionBankVo> find() {
        User user = UserContext.getCurrentUser();
        if (user == null || (user.getRoleId() != 1 && user.getRoleId() != 2)) {
            return null;
        } else {
            Integer teacherId = teacherService.getByUserId(user.getUserId()).getTeacherId();
            return this.list(new QueryWrapper<QuestionBank>().eq("teacher_id", teacherId)).stream().map(questionBank -> {
                QuestionBankVo vo = new QuestionBankVo();
                BeanUtils.copyProperties(questionBank, vo);
                String courseName = courseService.getById(questionBank.getCourseId()).getCourseName();
                vo.setCourse(courseName);
                String typeName = questionTypeService.getById(questionBank.getTypeId()).getTypeName();
                vo.setType(typeName);
                return vo;
            }).toList();
        }

    }

    @Override
    public AjaxJson add(QuestionBankVo questionBank) {
        User user = UserContext.getCurrentUser();
        if (user == null || (user.getRoleId() != 1 && user.getRoleId() != 2)) {
            return AjaxJson.getNotJur("权限不足");
        } else {
            Integer userId = user.getUserId();
            QuestionBank questionBank1 = new QuestionBank();
            BeanUtils.copyProperties(questionBank, questionBank1);
            questionBank1.setTeacherId(teacherService.getByUserId(userId).getTeacherId());
            return this.save(questionBank1) ? AjaxJson.getSuccess("添加成功") :AjaxJson.getError("添加失败");
        }
    }
}
